package org.example.guava;

import com.google.common.base.Function;
import com.google.common.collect.*;
import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Classname guavaDemo
 * @Description guavaDemo
 * @Date 2020/12/18 2:56 下午
 * @Created by xiangjd
 */
public class guavaDemo {
    public static void main(String[] args) {
        HashMap<Object, Object> hashMap = Maps.newHashMap();
        HashSet<Object> hashSet = Sets.newHashSet();
        List<Object> list = Lists.newArrayList();
        List<String> list1 = Lists.newArrayList("a", "v");
        Lists.newArrayListWithExpectedSize(100);
        Lists.newArrayListWithCapacity(100);
    }

    /**
     * 统计关键词出现的次数 容易出现npe
     */
    @Test
    public void test1() {
        String[] strings = new String[]{"a", "b", "c", "d", "a", "b", "c", "c"};
        HashMap<String, Integer> resultMap = new HashMap<>();
        for (String s : strings) {
            Integer integer = resultMap.get(s);
            if (integer == null) {
                resultMap.put(s, 1);
            } else {
                resultMap.put(s, integer + 1);
            }
        }
        System.out.println(resultMap);

        HashMultiset<String> multiset = HashMultiset.create();
        for (String s : strings) {
            multiset.add(s);
        }
        System.out.println("统计数组中a出现的次数:" + multiset.count("a"));
    }

    /**
     * map+list结构  {a->[1,2,3],b->[2,3,4],c->[5]}
     */
    @Test
    public void test2() {
        HashMultimap<String, Integer> hashMultimap = HashMultimap.create();
        hashMultimap.put("a", 1);
        hashMultimap.put("a", 2);
        hashMultimap.put("a", 3);
        hashMultimap.put("b", 1);
        hashMultimap.put("b", 3);
        Set<Integer> set = hashMultimap.get("a");
        System.out.println(set);
        System.out.println(hashMultimap);
    }

    /**
     * 双向map
     */
    @Test
    public void test3() {
        HashBiMap<String, String> biMap = HashBiMap.create();
        biMap.put("A", "C");
        System.out.println(biMap.get("A"));
        String c = biMap.inverse().get("C");
        System.out.println(c);
        biMap.forcePut("A", "D");
        System.out.println(biMap.get("A"));
        System.out.println(biMap.inverse().get(biMap.get("A")));
    }

    @Test
    public void test4() {
        List<String> list = Lists.newArrayList("a", "b", "c");
        List<String> newlist = Lists.transform(list, new Function<String, String>() {
            @Override
            public String apply(String s) {
                return s.toUpperCase();
            }
        });
        System.out.println(newlist);
        //和这个newList只是一个原list的一个视图,不能对其进行修改，否则报错或者修改丢失
//        newlist.add("c");
//        System.out.println(newlist);
    }

    /**
     * 交集并集差集
     */
    @Test
    public void test5() {
        Set<String> string1 = ImmutableSet.of("one", "two", "three", "four");
        Set<String> string2 = ImmutableSet.of("two", "three", "five", "six");
        //交集
        Sets.SetView<String> intersection = Sets.intersection(string1, string2);
        //并集
        Sets.SetView<String> union = Sets.union(string1, string2);
        //左差集
        Sets.SetView<String> difference1 = Sets.difference(string1, string2);
        //右差集
        Sets.SetView<String> difference2 = Sets.difference(string2, string1);
        Set<String> notupdateSet = intersection.immutableCopy();
        System.out.println(intersection);
        System.out.println(union);
        System.out.println(difference1);
        System.out.println(difference2);
        System.out.println(notupdateSet);
//        notupdateSet不可改变
        //notupdateSet.add("dd");
    }

    /**
     * 不可变集合天然内存小，由于只能读，多线程并发天然安全
     */
    @Test
    public void test6(){
        List<String> strings = Lists.newArrayList("a", "b", "c");
        System.out.println(ImmutableList.copyOf(strings));
        System.out.println(ImmutableList.of("d", "e"));
        ImmutableList<Object> f = ImmutableList.builder().add("f").addAll(strings).build();
        System.out.println(f);


    }
}
